<!-- 登录-->
<template>
	<view class="page">
		<view class="logo">
			<image src="../../static/logo.png" mode=""></image>
		</view>
		<!-- 填写区 -->
		<!--    <view class="input-info">
      <view class="info">
        <input type="tel" maxlength="11" v-model="form.phone" placeholder="手机号">
        <view class="more"></view>
      </view>
      <view class="info" :style="isLoginWay?'':'display: none'">
        <input type="tel" v-model="form.code" maxlength="6" placeholder="请输入验证码">
        <view class="more">
          <text class="mo">获取验证码</text>
          <text class="mo" style="display: none">59秒后重试</text>
        </view>
      </view>
      <view class="info" :style="isLoginWay?'display: none':''">
        <input :password='!isPassword' v-model="form.password" maxlength="26" placeholder="请输入密码">
        <view class="more">
          <text class="iconfont" :class="isPassword?'icon-eye-on':'icon-eye-off'" @click="isPassword = !isPassword"></text>
          <text class="mo">忘记密码</text>
        </view>
      </view>
    </view> -->
		<!-- 按钮 -->
		<!--  <view class="btn-info">
      <view class="btn" :style="isLogin?'opacity:1':'opacity:0.4'" @click="isLogin?onLogin():''">
        <text>登录</text>
      </view>
    </view> -->
		<!-- 操作 -->
		<!--   <view class="operation">
      <text @click="onLoginCut">{{isLoginWay?'手机号密码登录':'短信验证码登录'}}</text>
      <text @click="onRegister">新用户注册</text>
    </view> -->
		<!-- 其他方式登录 -->
		<view class="other-ways">
			<text>登录方式</text>
		</view>
		<!-- 登录方式 -->
		<view class="login-way" @click="getUserInfo">
			<view class="way">
				<image src="/static/wx_ico.png" mode=""></image>
				<text>微信登录</text>
			</view>
		</view>
	</view>
</template>

<script>

	import {
		baseUrl
	} from "@/config/baseUrl.js"
	export default {
		data() {
			return {
				preventClick: true,
				userInfo: {},
				Code: '',
				openid: '',
				session_key: '',
				isLogin: false,
				isLoginWay: false,
				isPassword: false,
				// 表单
				form: {
					nickName: '',
					phone: '',
					code: '',
					password: '',
				},
			};
		},
		methods: {
			/*
			 微信登录*/
			getUserInfo() {
				// 展示加载框
				uni.showLoading({
					title: '加载中',
				});
				let that = this;
				if (that.preventClick) {
					that.preventClick = false;
					wx.getUserProfile({
						desc: '登录后可同步数据',
						success: async (obj) => {
							that.preventClick = true;
							that.userInfo = obj.userInfo;
							uni.setStorageSync("nickName",that.userInfo.nickName)
							uni.setStorageSync("avatarUrl",that.userInfo.avatarUrl)
							// 调用 action ，请求登录接口
							uni.login({
								provider: 'weixin',
								success: async (res) => {
									that.Code = res.code;
									console.log('登录获取code', res.code);
									if (res.errMsg == 'login:ok') {
										uni.request({
											url: baseUrl + "/user/weChatLogin",
											method: 'POST',
											data: {
												code: that.Code,
												nickName: that.userInfo.nickName,
											},
											header: {
												"Content-Type": "application/x-www-form-urlencoded"
											},
											success: (res) => {
													console.log("登录信息111",res.data.result)
												if (res.data.result==null) {												
													uni.showToast({
														title: '需要注册',
														icon: 'error',
														mask: true,
													});
													console.log("需要注册")
													uni.navigateTo({
														url: '/pages/register/register',
													})
												} else {
													uni.showToast({
														title: '登录成功',
														icon: 'success',
														mask: true,
													});
													console.log("登录用户信息：",res.data.result)
													console.log("登录用户ID：",res.data.userId)
													uni.setStorageSync("userId",res.data.userId)
													uni.setStorageSync("userInfo",res.data.result)
													uni.switchTab({
														url:'/pages/home/home'
													})
												}
											},
										})
									}
								}
							})
						},
						fail: (err) => {
							uni.showToast({
								title: '授权已取消',
								icon: 'error',
								mask: true,
							});
							console.log(err);
						},
						complete: () => {
							// 隐藏loading
							uni.hideLoading();
						},
					})
				}
			},
			/**
			 * 登录切换
			 */
			onLoginCut() {
				this.isLoginWay = !this.isLoginWay;
				// 验证码
				if (this.isLoginWay) {
					this.isLogin = this.form.code && this.form.phone ? true : false;
				}
				// 账号密码
				if (!this.isLoginWay) {
					this.isLogin = this.form.password && this.form.phone ? true : false;
				}
			},
			/**
			 * 登录点击
			 */
			onLogin() {},
		},
		watch: {
			form: {
				handler(newValue, oldValue) {
					// 验证码
					if (this.isLoginWay) {
						this.isLogin = newValue.code && newValue.phone ? true : false;
					}
					// 账号密码
					if (!this.isLoginWay) {
						this.isLogin = newValue.password && newValue.phone ? true : false;
					}
				},
				deep: true
			}
		}
	}
	export function loginAuth(data) {
		return request({
			url: '/wx/user/' + data.appid + '/login/',
			data: {
				code: data.code,
			},
		});
	}
</script>

<style scoped lang="scss">
	@import 'login.scss';
</style>
